//
//  DealerDataManage.m
//  WGFGlobalHouses
//
//  Created by welldone on 15/5/14.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "DealerDataManage.h"
#import "SingletonData.h"
#import "UserInfoData.h"
#import "StringTool.h"
#import "UserModal.h"
#import "Tools.h"
#import "API.h"


@implementation DealerDataManage


+(void)saveUserInfo:(UserInfoData*)data{
    
    if ([StringTool isBlankString:data.consumerID]) {
        data.companyInfo = @"";
    }
    
    if ([StringTool isBlankString:data.moneyCount]) {
        data.moneyCount = @"0";
    }
    
    if ([StringTool isBlankString:data.nickName]) {
        data.nickName = @"无数据";
    }
    
    if ([StringTool isBlankString:data.headImgUrl]) {
        data.headImgUrl = @"";
    }
    
    if ([StringTool isBlankString:data.companyName]) {
        data.companyName = @"无数据";
    }
    
    
    if ([StringTool isBlankString:data.mobile]) {
        data.mobile = @"未绑定";
    }
    
    if ([StringTool isBlankString:data.mail]) {
        data.mail = @"无数据";
    }
    
    if ([StringTool isBlankString:data.tell]) {
        data.tell = @"无数据";
    }
    
    if ([StringTool isBlankString:data.cityName]) {
        data.cityName = @"无数据";
    }
    
    if ([StringTool isBlankString:data.areaName]) {
        data.areaName = @"无数据";
    }
    
    if ([StringTool isBlankString:data.postCode]) {
        data.postCode = @"无数据";
    }
    
    if ([StringTool isBlankString:data.address]) {
        data.address = @"无数据";
    }
    
    if ([StringTool isBlankString:data.companyInfo]) {
        data.companyInfo = @"无数据";
    }
    
    if ([StringTool isBlankString:data.validateName]) {
        data.validateName = @"未认证";
    }
    
    if ([StringTool isBlankString:data.agencyCode]) {
        data.agencyCode = @"0";
    }
    if ([StringTool isBlankString:data.account]) {
        data.account = @"无数据";
    }

    
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    [defaults setObject:data.consumerID forKey:@"userId"];
    [defaults setObject:[NSNumber numberWithInteger:data.ordersCount] forKey:@"orders"];
    [defaults setObject:[NSNumber numberWithInteger:data.storesCount] forKey:@"stores"];
    [defaults setObject:[NSNumber numberWithInteger:data.brokersCount] forKey:@"brokers"];
    [defaults setObject:data.moneyCount forKey:@"brokerages"];
    [defaults setObject:data.nickName forKey:@"nickname"];
    [defaults setObject:data.headImgUrl forKey:@"avatar"];
    [defaults setObject:data.companyName forKey:@"company"];
    [defaults setObject:data.companyInfo forKey:@"brief"];
    [defaults setObject:data.mobile forKey:@"mobile"];
    [defaults setObject:data.mail forKey:@"mail"];
    [defaults setObject:data.tell forKey:@"tel"];
    [defaults setObject:data.cityName forKey:@"cityName"];
    [defaults setObject:data.areaName forKey:@"areaName"];
    [defaults setObject:data.postCode forKey:@"area"];
    [defaults setObject:data.address forKey:@"address"];
    [defaults setObject:data.validateName forKey:@"validateName"];
    [defaults setObject:data.agencyCode forKey:@"agencyCode"];
    [defaults setObject:data.account forKey:@"account"];
    [[NSUserDefaults standardUserDefaults]synchronize];
}



+(UserInfoData*)getUserInfo{
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    UserInfoData *data = [[UserInfoData alloc]init];

    
    [data setConsumerID:[defaults objectForKey:@"userId"]];
    [data setOrdersCount:[[defaults objectForKey:@"orders"] integerValue]];
    [data setStoresCount:[[defaults objectForKey:@"stores"] integerValue]];
    [data setBrokersCount:[[defaults objectForKey:@"brokers"] integerValue]];
    [data setMoneyCount:[defaults objectForKey:@"brokerages"]];
    [data setNickName:[defaults objectForKey:@"nickname"]];
    [data setHeadImgUrl:[defaults objectForKey:@"avatar"]];
    [data setCompanyName:[defaults objectForKey:@"company"]];
    
    [data setMobile:[defaults objectForKey:@"mobile"]];
    [data setMail:[defaults objectForKey:@"mail"]];
    [data setTell:[defaults objectForKey:@"tel"]];
    [data setCityName:[defaults objectForKey:@"cityName"]];
    [data setAreaName:[defaults objectForKey:@"areaName"]];
    [data setPostCode:[defaults objectForKey:@"area"]];
    [data setAddress:[defaults objectForKey:@"address"]];
    [data setValidateName:[defaults objectForKey:@"validateName"]];
    [data setCompanyInfo:[defaults objectForKey:@"brief"]];
    [data setAgencyCode:[defaults objectForKey:@"agencyCode"]];
    [data setAccount:[defaults objectForKey:@"account"]];
    
    return data;
}



-(ResulteMod*)refreshUserInfo{
    
    NSString *url = [NSString stringWithFormat:@"%@%ld",[API urlMake:UserInfo_jxs],(long)[UserModal shareUserModel].userId];
    
    ResulteMod *mod = [Connect doGetWithOutCaChe:url];
    
    
    //返回格式不同 需要单独处理
    NSString *code = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    if ([StringTool isBlankString:code]) {
         mod.resultCode = @"";
    }else{
         mod.resultCode = code;
    }
    mod.resultTip = mod.resultDic[@"message"];
    
    
    
    if (![mod.resultCode isEqualToString:@"20003"]){
        mod.resltState = ResultFail;
        return mod;
    }
    
    NSDictionary *resDic = mod.resultDic;
    NSDictionary *infoDic = resDic[@"infoDto"];
    
    UserInfoData *user = [[UserInfoData alloc]init];
    [user setConsumerID:[NSString stringWithFormat:@"%@",infoDic[@"consumerId"]]];
    
    
    [user setOrdersCount:[infoDic[@"orders"] intValue]];
    
    [user setStoresCount:[infoDic[@"stores"] intValue]];

    [user setBrokersCount:[infoDic[@"brokers"] intValue]];
    
    
    [user setMoneyCount:infoDic[@"brokerages"]];
    
    [user setNickName:infoDic[@"nickname"]];
//    NSString *imageForwad = @"http://files.wgfcdn.com/";
    
    if([StringTool isBlankString:infoDic[@"avatar"]]){
        
        [user setHeadImgUrl:@""];
    }else{
        [user setHeadImgUrl:[API videoUrlMake:infoDic[@"avatar"]]];
    }
    
    [user setCompanyName:infoDic[@"company"]];
    [user setMobile:infoDic[@"mobile"]];
    [user setMail:infoDic[@"mail"]];
    [user setTell:infoDic[@"tel"]];
    [user setCityName:infoDic[@"cityName"]];
    [user setAreaName:infoDic[@"areaName"]];
    [user setAddress:infoDic[@"address"]];
    [user setPostCode:infoDic[@"area"]];
    [user setCompanyInfo:infoDic[@"brief"]];
    [user setAgencyCode:infoDic[@"agencyCode"]];
    
    [user setValidate:[infoDic[@"validate"] intValue]];
    
    [user setValidateName:infoDic[@"validateName"]];
    [user setAccount:infoDic[@"account"]];
    
    [DealerDataManage saveUserInfo:user];
    
    
    
    return mod;
}



-(void)upUserInfo:(UserInfoData*)data withCallBack:(FinishCallBack)block{

    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       
        
        NSString *url = [NSString stringWithFormat:@"%@/%ld",[API urlMake:UserInfo_jxs],(long)[UserModal shareUserModel].userId];
        
        ResulteMod *mod = [Connect doGetWithOutCaChe:url];
        
        //返回格式不同 需要单独处理
        NSString *code = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
        if ([StringTool isBlankString:code]) {
            mod.resultCode = @"";
        }else{
            mod.resultCode = code;
        }
        mod.resultTip = mod.resultDic[@"message"];
        
        if (![mod.resultCode isEqualToString:@"20003"]){
            mod.resltState = ResultFail;
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            block(mod);
        });
    });
}



-(void)upHeadImg:(UIImage*)img View:(UIButton*)head{


    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    //  manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    
    
    NSData *imageData = UIImageJPEGRepresentation(img, 1);
    NSDictionary *parameters =@{@"consumerId":[NSNumber numberWithLong:[UserModal shareUserModel].userId]};
    
    [manager POST:[API urlMake:HeadImgUp] parameters:parameters constructingBodyWithBlock:^(id formData) {
        
        [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"application.jpg" mimeType:@"image/jpeg"];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [head setBackgroundImage:img forState:UIControlStateNormal];
        [UserModal shareUserModel].img = img;
        
        NSLog(@"头像设置成功!");
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:@"头像设置失败!"];
        NSLog(@"头像设置失败!");
    }];
}



-(ResulteMod*)refreshCommissionHeadData:(NSDictionary*)dic{

    ResulteMod *mod = [Connect doPostOutCaCheWithUrl:[API urlMake:CommissionManage] withParamDic:dic];
//    NSString *url = [API urlMake:CommissionManage];
    if (![mod.resultCode isEqualToString:@"20003"]){
        mod.resltState = ResultFail;
    }else{
        
        NSDictionary *dic = [mod.resultDic objectForKey:@"borkerageStatisticDto"];
        CommissionData *data = [CommissionData new];
        [data setSaleMoneryCount:[NSString stringWithFormat:@"$%@",[Tools getFormNumber:dic[@"totalSaledSum"]]]];
        [data setPassCommissionMoneryCount:[NSString stringWithFormat:@"$%@",[Tools getFormNumber:dic[@"payedTotalBorkerageSum"]]]];
        [data setUnPassCommissionMoneryCount:[NSString stringWithFormat:@"$%@",[Tools getFormNumber:dic[@"totalBorkerageSum"]]]];
        [data setCommissionMoneryCount:[Tools getFormNumber:dic[@"totalBorkerageSum"]]];
        [data setCommissionRate:dic[@"avgBrokerageRate"]];
        [data setSaleHourseCount:[NSString stringWithFormat:@"%@",[Tools getFormNumber:[NSNumber numberWithInteger:[dic[@"totalSaledCount"] integerValue]]]]];
        [data setCELL_STYLE:CELL_STYLE_HEAD];
        
        [mod setDataMod:data];
    }

    return mod;

}



-(void)refreshYongJinDataTableIndex:(Commission_tab)tab withParm:(NSMutableDictionary *)dic  CallBack:(FinishCallBack)block{
    

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        [dic setObject:[UserModal shareUserModel].userCode forKey:@"agencyCode"];
        
        if(tab==Commission_tab0){
        }else if (tab==Commission_tab1){
            
            NSDate *now = [NSDate date];
            NSCalendar *calendar = [NSCalendar currentCalendar];
            NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
            NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
            
            NSInteger year = [dateComponent year];
            NSInteger month = [dateComponent month];
            
            [dic setObject:[NSString stringWithFormat:@"%ld-%ld-01",(long)year,(long)month] forKey:@"startTime"];
            
            if (month==12) {
                month=1;
                year++;
            }else{
                month++;
            }
            
            [dic setObject:[NSString stringWithFormat:@"%ld-%ld-01",(long)year,(long)month]  forKey:@"endTime"];
            
            
        }else if (tab==Commission_tab2){
            
            
            NSDate *now = [NSDate date];
            NSCalendar *calendar = [NSCalendar currentCalendar];
            NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
            NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
            
            NSInteger year = [dateComponent year];
            NSInteger month = [dateComponent month];
            
            [dic setObject:[NSString stringWithFormat:@"%ld-%ld-01",(long)year,(long)month]  forKey:@"endTime"];
            
            if (month==1) {
                month=12;
                year--;
            }else{
                month--;
            }
            
            [dic setObject:[NSString stringWithFormat:@"%ld-%ld-01",(long)year,(long)month] forKey:@"startTime"];
        }

        
        ResulteMod *mod = [self refreshCommissionHeadData:dic];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            block(mod);
            
        });
    });
}











-(void)refreshYongJinListDataPageIndex:(int)page parm:(NSMutableDictionary *)dic tableIndex:(Commission_tab)tab CallBack:(FinishCallBack)block{
    

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        [dic setObject:[UserModal shareUserModel].userCode forKey:@"agencyCode"];
        
        if(tab==Commission_tab0){
        }else if (tab==Commission_tab1){
        
            NSDate *now = [NSDate date];
            NSCalendar *calendar = [NSCalendar currentCalendar];
            NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
            NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
            
            NSInteger year = [dateComponent year];
            NSInteger month = [dateComponent month];

            [dic setObject:[NSString stringWithFormat:@"%ld-%.2ld-01",(long)year,(long)month] forKey:@"startTime"];
            
            if (month==12) {
                month=1;
                year++;
            }else{
                month++;
            }
            
            [dic setObject:[NSString stringWithFormat:@"%ld-%.2ld-01",(long)year,(long)month]  forKey:@"endTime"];
            
            
        }else if (tab==Commission_tab2){
        
            
            NSDate *now = [NSDate date];
            NSCalendar *calendar = [NSCalendar currentCalendar];
            NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
            NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
            
            NSInteger year = [dateComponent year];
            NSInteger month = [dateComponent month];
            
            [dic setObject:[NSString stringWithFormat:@"%ld-%.2ld-01",(long)year,(long)month]  forKey:@"endTime"];
            
            if (month==1) {
                month=12;
                year--;
            }else{
                month--;
            }
            
            [dic setObject:[NSString stringWithFormat:@"%ld-%.2ld-01",(long)year,(long)month] forKey:@"startTime"];
        }
        
        

        NSString *url = [NSString stringWithFormat:@"%@%d/10",[API urlMake:CommissionTJ],page];
        
        ResulteMod *mod = [Connect doPostOutCaCheWithUrl:url withParamDic:dic];
#warning 佣金统计未开启缓存
//        ResulteMod *mod = [Connect doPostCaCheWithUrl:url withParamDic:dic];
        
        NSMutableArray *array = [NSMutableArray new];
        [mod setDataMod:array];
        
        if (![mod.resultCode isEqualToString:@"20003"]){
            mod.resltState = ResultFail;
            

            dispatch_async(dispatch_get_main_queue(), ^{
                
                block(mod);
            });

        }else{
            
            NSArray *dataArray  =  mod.resultDic[@"brokerageDtos"];
            
            for (NSDictionary *dic in dataArray) {
                
                CommissionData *data = [CommissionData new];
                
                
                [data setAddress:dic[@"premisesHousingTitle"]];
                
                [data setPrice:[Tools getFormNumber:dic[@"salePrice"]]];
                [data setExceptCommission:[Tools getFormNumber:dic[@"brokerageBudget"]]];
                [data setActualCommission:[Tools getFormNumber:dic[@"brokerageTotal"]]];
                
                [data setCELL_STYLE:CELL_STYLE_CELL];
                [data setTitle:dic[@"premisesTitle"]];
                [data setName:dic[@"disUserName"]];
                
                if([dic[@"disUserConsumerId"] isKindOfClass:[NSNull class]]){
                    [data setBrokerId:0];
                }else{
                    [data setBrokerId:[dic[@"disUserConsumerId"] intValue]];
                }
                
                [data setTime:[NSString stringWithFormat:@"下单时间:%@",dic[@"orderTime"]]];
                [data setCompany:dic[@"orgDepName"]];
                [data setCompanyId:[NSString stringWithFormat:@"%@",dic[@"orgDepCode"]]];
                [data setOrderCode:[NSString stringWithFormat:@"订单编号:%@",dic[@"orderCode"]]];
                [data setCommissionRate:dic[@"brokerageRate"]];
                [data setOrderState:dic[@"brokerageStatusName"]];
                
                
                
                [array addObject:data];
                
            }
            
            
            if (array.count>0&&page==1) {//读取第一页的时候才加载
                //headData
                [self refreshYongJinDataTableIndex:tab withParm:dic CallBack:^(ResulteMod *resultMod) {
                    
                    if(resultMod.resltState==ResultNetError){
                        
                    }else if (resultMod.resltState==ResultFail){
                        
                    }else{
                        
                        CommissionData *headData = resultMod.dataMod;
                        [array insertObject:headData atIndex:0];
                    }
                    
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                        block(mod);
                    });
                    
                }];
                
            }else{
            
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    block(mod);
                });
            }
        }
    });
}



-(void)refreshStoreCommissionDataCallBack:(FinishCallBack)block{
    

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        
        NSString *url = [NSString stringWithFormat:@"%@%@",[API urlMake:StoreCommissionList],[UserModal shareUserModel].userCode];
        
        ResulteMod *mod = [Connect doGetWithCaChe:url];
        
        NSMutableArray *array = [NSMutableArray new];
        [mod setDataMod:array];
        
        if (![mod.resultCode isEqualToString:@"20003"]){
            mod.resltState = ResultFail;
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                block(mod);
            });
            
        }else{
            
            NSArray *dataArray  =  mod.resultDic[@"manageListDtos"];
            
            for (NSDictionary *dic in dataArray) {
                
                StoreCommission *data = [StoreCommission new];
                [data setStoreName:dic[@"storeName"]];
                [data setSaleCount:[Tools getFormNumber:[NSNumber numberWithInt:[dic[@"brokerages"] intValue]]]];
                [data setNumber:[NSString stringWithFormat:@"%@",dic[@"brokeCounts"]]];
                [data setStoreCode:[NSString stringWithFormat:@"%@",dic[@"storeCode"]]];
                [data setCELL_STYLE:CELL_STYLE_CELL];
                [array addObject:data];
            }
            
            NSDictionary *dic = [NSDictionary dictionaryWithObject:[UserModal shareUserModel].userCode forKey:@"agencyCode"];
            
            ResulteMod *resultMod = [self refreshCommissionHeadData:dic];
            
            
            
            if(resultMod.resltState==ResultNetError){

            }else if (resultMod.resltState==ResultFail){

            }else{
                
                CommissionData *headData;
                headData = resultMod.dataMod;
                [array insertObject:headData atIndex:0];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                block(mod);
            });
        }
    });
}



-(void)refreshStoreCommissionDetailData:(int)page storeId:(NSString*)code  CallBack:(FinishCallBack)block{

    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setObject:code forKey:@"storeCode"];
        [dic setObject:@"1" forKey:@"isBrokerageShow"];
        
        NSString *url = [NSString stringWithFormat:@"%@%d/10",[API urlMake:StoreCommissionDetail],page];
        
        ResulteMod *mod = [Connect doPostOutCaCheWithUrl:url withParamDic:dic];
        
        NSMutableArray *array = [NSMutableArray new];
        [mod setDataMod:array];
        
        if (![mod.resultCode isEqualToString:@"20003"]){
            mod.resltState = ResultFail;
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                block(mod);
            });
            
        }else{
            
            NSArray *dataArray  =  mod.resultDic[@"brokersDtos"];
            
            for (NSDictionary *dic in dataArray) {
                
                StoreCommissionBroker *data = [StoreCommissionBroker new];
                [data setName:dic[@"brokerName"]];
                [data setMobile:[NSString stringWithFormat:@"%@",dic[@"brokerMobile"]]];
                
                [data setImgUrL:[StringTool isBlankString:dic[@"brokerAvatar"]]?@"":[API imgUrlMake:dic[@"brokerAvatar"]]];
                [data setBrokerId:[dic[@"brokerId"] intValue]];
                [data setSaleCount:[StringTool isBlankString:dic[@"brokerages"]]?@"0":dic[@"brokerages"]];
                [data setStoreCode:code];
                [array addObject:data];
            }
            
            
            if (array.count>0&&page==1) {//读取第一页的时候才加载
                
                NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:code,@"storeCode",[UserModal shareUserModel].userCode,@"agencyCode", nil];
                

                ResulteMod *resultMod = [self refreshCommissionHeadData:dic];
                
                CommissionData *headData;
                if(resultMod.resltState==ResultNetError){
                    headData = [CommissionData new];
                }else if (resultMod.resltState==ResultFail){
                    headData = [CommissionData new];
                }else{
                    headData = resultMod.dataMod;
                }
                
                [array insertObject:headData atIndex:0];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    block(mod);
                });
                
            }else{
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    block(mod);
                });
            }
        }
    });
}



-(void)refreshBrokerCommissionDetailData:(int)page data:(NSDictionary *)data  CallBack:(FinishCallBack)block{

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
       
        NSString *url = [NSString stringWithFormat:@"%@%d/10",[API urlMake:CommissionTJ],page];
        
        ResulteMod *mod = [Connect doPostOutCaCheWithUrl:url withParamDic:data];
        

        NSMutableArray *array = [NSMutableArray new];
        [mod setDataMod:array];
    

        
        if (![mod.resultCode isEqualToString:@"20003"]){
            mod.resltState = ResultFail;
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                block(mod);
            });
            
        }else{
            
            NSArray *dataArray  =  mod.resultDic[@"brokerageDtos"];
            
            for (NSDictionary *dic in dataArray) {
                
                CommissionData *data = [CommissionData new];
                
                [data setAddress:dic[@"premisesHousingTitle"]];
                
                [data setPrice:[Tools getFormNumber:dic[@"salePrice"]]];
                [data setExceptCommission:[Tools getFormNumber:dic[@"brokerageBudget"]]];
                [data setActualCommission:[Tools getFormNumber:dic[@"brokerageTotal"]]];
                
                [data setCELL_STYLE:CELL_STYLE_CELL];
                [data setTitle:[StringTool isBlankString:dic[@"premisesTitle"]]?@"无数据":dic[@"premisesTitle"]];
                [data setTime:[NSString stringWithFormat:@"下单时间:%@",dic[@"orderTime"]]];
                [data setOrderCode:[NSString stringWithFormat:@"订单编号:%@",dic[@"orderCode"]]];
                [data setCommissionRate:dic[@"brokerageRate"]];
                [data setOrderState:dic[@"brokerageStatusName"]];
                
                [array addObject:data];
            }
            
            
            if (array.count>0&&page==1) {//读取第一页的时候才加载
                
          
        
                ResulteMod *resultMod = [self refreshCommissionHeadData:data];
                
                CommissionData *headData;
                if(resultMod.resltState==ResultNetError){
                    headData = [CommissionData new];
                }else if (resultMod.resltState==ResultFail){
                    headData = [CommissionData new];
                }else{
                    headData = resultMod.dataMod;
                }
                
                [array insertObject:headData atIndex:0];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    block(mod);
                });
                
            }else{
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    block(mod);
                });
            }
        }
    });
}



-(void)refreshBrokerCommissionListData:(int)page CallBack:(FinishCallBack)block{
    
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setObject:[UserModal shareUserModel].userCode forKey:@"agencyCode"];
        [dic setObject:@"1" forKey:@"isBrokerageShow"];
        
        NSString *url = [NSString stringWithFormat:@"%@%d/10",[API urlMake:StoreCommissionDetail],page];
        
        ResulteMod *mod = [Connect doPostOutCaCheWithUrl:url withParamDic:dic];
        
        NSMutableArray *array = [NSMutableArray new];
        [mod setDataMod:array];

        
        if (![mod.resultCode isEqualToString:@"20003"]){
            mod.resltState = ResultFail;
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                block(mod);
            });
            
        }else{
            
            NSArray *dataArray  =  mod.resultDic[@"brokersDtos"];
            
            for (NSDictionary *dic in dataArray) {
                
                StoreCommissionBroker *data = [StoreCommissionBroker new];
                [data setName:dic[@"brokerName"]];
                [data setMobile:[NSString stringWithFormat:@"%@",dic[@"brokerMobile"]]];
                [data setImgUrL:[StringTool isBlankString:dic[@"brokerAvatar"]]?@"":[API imgUrlMake:dic[@"brokerAvatar"]]];
                [data setBrokerId:[dic[@"brokerId"] intValue]];
                [data setSaleCount:[StringTool isBlankString:dic[@"brokerages"]]?@"0":dic[@"brokerages"]];
                [array addObject:data];
            }
            
            
            if (array.count>0&&page==1) {//读取第一页的时候才加载
                
                NSMutableDictionary *dic = [NSMutableDictionary new];
                [dic setObject:[UserModal shareUserModel].userCode forKey:@"agencyCode"];
                
                ResulteMod *resultMod = [self refreshCommissionHeadData:dic];
                
                CommissionData *headData;
                if(resultMod.resltState==ResultNetError){
                    headData = [CommissionData new];
                }else if (resultMod.resltState==ResultFail){
                    headData = [CommissionData new];
                }else{
                    headData = resultMod.dataMod;
                }
                
                [array insertObject:headData atIndex:0];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    block(mod);
                });
                
            }else{
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    block(mod);
                });
            }
        }
    });
}



@end
